package com.example.red_tea_house.moudle.Home.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.red_tea_house.MyApplication;
import com.example.red_tea_house.R;
import com.example.red_tea_house.entity.UserBean;
import com.example.red_tea_house.entity.setRecyclerViewNameBean;
import com.example.red_tea_house.moudle.Home.adapter.HomeAdapter;
import com.example.red_tea_house.moudle.SystemSetting.View.SystemSettingActivity;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;


public class HomeFragment extends Fragment {
    private View rootView;

    private List<setRecyclerViewNameBean> data = new ArrayList<>();
    setRecyclerViewNameBean bean = new setRecyclerViewNameBean();
    setRecyclerViewNameBean bean2 = new setRecyclerViewNameBean();
    setRecyclerViewNameBean bean3 = new setRecyclerViewNameBean();
    setRecyclerViewNameBean bean4 = new setRecyclerViewNameBean();
    setRecyclerViewNameBean bean5 = new setRecyclerViewNameBean();


    Context context=MyApplication.getContext();
    public HomeFragment() {

    }


    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView=inflater.inflate(R.layout.main_page_layout,container,false);
        initHomePageItem();
        return rootView;
    }
    private void initHomePageItem(){
        getUserData();
        getItemBeans();

        RecyclerView recyclerView = rootView.findViewById(R.id.home_choice_rv);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(linearLayoutManager);

        HomeAdapter homeAdapter = new HomeAdapter(data, context,HomeFragment.this);
        recyclerView.setAdapter(homeAdapter);
    }
    public void getUserData(){
        List<UserBean> userBean= LitePal.findAll(UserBean.class);
        AppCompatTextView appCompatTextView=rootView.findViewById(R.id.nickname_btn);
        Button button=rootView.findViewById(R.id.autograph_btn_1);
        if(!userBean.isEmpty()){
            appCompatTextView.setText(userBean.get(0).getNickname());
            button.setText(userBean.get(0).getAutograph());
        }
    }
    private void getItemBeans(){
        bean.setName("个人空间");data.add(bean);
        bean2.setName("收藏夹");data.add(bean2);
        bean3.setName("浏览历史");data.add(bean3);
        bean4.setName("时光记忆");data.add(bean4);
        bean5.setName("系统设置");data.add(bean5);
    }

    public void turnPage(String pageName){
        if(pageName.equals("系统设置")){
            Intent intent=new Intent(context, SystemSettingActivity.class);
            startActivity(intent);
        }
    }
}